<?php
/*
 * @Description: @河南道本
 * @Author: su
 * @Email: 123@123.com
 * @Date: 2020-11-12 18:19:19
 * @LastEditTime: 2020-11-12 18:21:49
 * @LastEditors: li
 */

namespace app\admin\model;

use app\admin\model\MeetType;
use think\Model;
use app\admin\model\Baoming;


/**
 * 会务模型
 * @class   Meet
 */
class Meet extends Model
{


    // 表名
    protected $name = 'meeting';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'start_time_text',
        'end_time_text',
        'status_text',
        'type_text'
    ];



    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2'),'3' => __('Type 3'), '4' => __('Type 4'),'5' => __('Type 5'), '6' => __('Type 6')];
    }



    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }



    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setIdentityAttr($value)
    {
        return $value === '' ? null : implode(',',$value);
    }
    protected function getIdentityAttr($value)
    {
        return explode(',',$value);
    }
    protected function setIdentityPriceAttr($value)
    {
        return json_encode($value);
    }
    protected function getIdentityPriceAttr($value)
    {
        return $value ? json_decode($value,true) : null;
    }
    /**
     * 获取此会务报名记录
     */
    public function type()
    {
        return $this->hasOne(MeetType::class, 'id', 'type')->bind(['type_name'=>'name']);
    }

    /**
     * 获取此会务报名记录
     */
    public function baoming()
    {
        return $this->hasMany(Baoming::class, 'mid', 'id')
            ->order('id DESC')
            ->where('is_pay', Baoming::IS_PAY_TRUE);
    }

    /**
     * 获取签到记录
     */
    public function signed()
    {
        return $this->hasMany(Baoming::class, 'mid', 'id')
            ->order('id DESC')
            ->where('is_check', 1)->where('is_pay', Baoming::IS_PAY_TRUE);
    }

    /**
     * 获取签单记录
     */
    public function signbill()
    {
        return $this->hasMany(SignBill::class, 'mid', 'id')
            ->order('id DESC')
            ->where('is_pay', SignBill::IS_PAY_TRUE);
    }

    /**
     * 获取奖品兑换记录
     */
    public function prizeLog()
    {
        return $this->hasMany(DrawLog::class, 'mid', 'id')
            ->order('id DESC')
            ->where('status', DrawLog::STATUS_REDEEMED);
    }
	/**
	 * 关联 优惠券 数据
	 * @return \think\model\relation\BelongsTo
	 */
    public function voucher()
	{
		return $this->belongsTo(Voucher::class, 'voucher_id', 'id');
	}
}
